home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Interapplication Communication / AECDEV⁄AEDAEMON / AECdev.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  10.0 KB  |  221 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #   Apple Developer Technical Support
  4. #
  5. #   AppleEvent Sample Control Panel Device
  6. #
  7. #   AECDEV
  8. #
  9. #   AECDEV.c  -   C Source
  10. #   Written by  C.K. Haun
  11. #
  12. #   Copyright © 1991 Apple Computer, Inc.
  13. #   All rights reserved.
  14. #
  15. #   Versions:   1.0                 8/91
  16. #
  17. #   Components: AECdev.p          August 2, 1991
  18. #               AECDEV.PPC.c      August 2, 1991
  19. #
  20. #   AECDEV demonstrates the techniques needed to send AppleEvents
  21. #   from a CDEV/DA/INIT/Driver.
  22. #   Requires the sample AEDaemon to work.
  23. #   
  24. #   The techniques used in AECdev are the same you need to use to send AppleEvents
  25. #   from any non-application piece of code, DAs/INIT/Drivers, or anything else.
  26. #   Since the High Level Event manager will not let anyone post High Level Events 
  27. #   unless they have an event loop, your non-event looped code must rely
  28. #   on a 'buddy' (in this case AEDaemon) to do the sending for you.
  29. #   There's not really much more code to write (just some PPC code), it's
  30. #   mainly a matter of moving the code from the CDEV to the backgrounder.
  31. #
  32. #   Using a buddy and PPC can give you all the functionality of AppleEvents.
  33. ------------------------------------------------------------------------------*/
  34. /*------------------------------------------------------------------------------
  35. #   This file contains the basic CDEV controlling code
  36. ------------------------------------------------------------------------------*/
  37.  
  38. #define __BUILDINGCDEV__
  39. #define __AECDEV__
  40.  
  41. #include "NonAppAEVT.h"
  42.  
  43. /* This is the main dispatcher. It must be the first code in the cdev.
  44. AECDEV's dispatcher responds only to the following messages from
  45. the Control Panel:
  46.  
  47. macDev      - To indicate what machines it is available on.
  48. initDev     - To set up some temporary storage and get the caret started.
  49.  
  50. */
  51. pascal Handle AECDEV(short message, short item, short numItems, short CPanelID, EventRecord *theEvent, Handle cdevStorage,
  52.                      DialogPtr CPDialog)
  53. {
  54.     CDEVPtr temp;
  55. #pragma unused (CPanelID,theEvent)     /* unused formal parameters */    
  56.     if (message == macDev)
  57.         return((Handle)1);                                  /* we work on every machine */
  58.     else if (cdevStorage != nil) {
  59.         switch (message) {
  60.             long result;
  61.             case initDev:                                   /* initialize cdev */
  62.                 cdevStorage = NewHandle(sizeof(CDEVRec));       /* create private storage */
  63.                 /* get a PPC block.  If we can't allocate one, we'll just be dead */
  64.                 if (cdevStorage) {
  65.                     HLock(cdevStorage);
  66.                     temp = (CDEVPtr)*cdevStorage;
  67.                     temp->myPPCBlock = (MyPPCRecPtr)NewPtrClear(sizeof(MyPPCRec));
  68.                     if (temp->myPPCBlock) {
  69.                         temp->myPPCBlock->myPort = (PPCPortPtr)NewPtrClear(sizeof(PPCPortRec));
  70.                         if (temp->myPPCBlock->myPort) {
  71.                         /* set the default settings for my blocks */
  72.                             temp->myPPCBlock->buffer = nil;
  73.                             temp->myPPCBlock->bufferSize = nil;
  74.                             temp->myPPCBlock->ourPort = nil;
  75.                             temp->myPPCBlock->dataToXfer = nil;
  76.                             temp->myPPCBlock->currentSessionRef = nil;
  77.                             temp->searchForTarget = false;
  78.                             temp->noBuddy = false;
  79.                             temp->notSys7 = false;
  80.                             temp->eventPending = false;
  81.                             HUnlock(cdevStorage);
  82.                             HiliteControl(SnatchHandle(CPDialog, numItems + 1), 255);
  83.                             /* check Gestalt to see if we're on an AppleEvent/PPC */
  84.                             /* capable machine machine */
  85.                             if ((Gestalt(gestaltAppleEventsAttr, &result) != noErr) ||
  86.                                 (Gestalt(gestaltPPCToolboxAttr, &result) != noErr)) {
  87.                                 /* no PPC or no AppleEvents.  Forget it */
  88.                                 temp = (CDEVPtr)*cdevStorage;
  89.                                 temp->notSys7 = true;
  90.                             } else {
  91.                             /* Start PPC for us */
  92.                                 if (PPCInit() == noErr) {
  93.                                     FindAEBuddy((CDEVHnd)cdevStorage);/* find and launch AEBud */
  94.                                 } else {
  95.                                 /* PPC didn't init correctly, bail out */
  96.                                     temp = (CDEVPtr)*cdevStorage;
  97.                                     temp->notSys7 = true;
  98.                                 }
  99.                             }
  100.                             temp = (CDEVPtr)*cdevStorage;
  101.                             /* Alert user that we con't find our Buddy */
  102.                             if (temp->noBuddy)
  103.                                 StopAlert(kNoBuddyAlert, nil);
  104.                         } else {                            /* PPC Port rec mem check */
  105.                             /* if this fails, kill the other pointer also */
  106.                             DisposPtr((Ptr)temp->myPPCBlock);
  107.                             temp->myPPCBlock = nil;
  108.                             DisposHandle(cdevStorage);
  109.                             /* and have Control Panel manager say out of mem */
  110.                             cdevStorage = nil;
  111.                         }
  112.                     } else {                                /* block mem check */
  113.                         DisposHandle(cdevStorage);
  114.                         /* and have Control Panel manager say out of mem */
  115.                         cdevStorage = nil;
  116.                     }
  117.                 }                                           /* cdevStorage mem check */
  118.                 break;
  119.             case nulDev:
  120.                 /* check here all the time */
  121.                 /* for the port coming up if we had to launch the sender */
  122.                 /* also dim the control, as necessary */
  123.                 /* Can't do this in our completion routine, since it requires calling the */
  124.                 /* COntrol Manager and the Memory Manager */
  125.                 HLock((Handle)cdevStorage);
  126.                 temp = (CDEVPtr)*cdevStorage;                
  127.                 if (temp->eventPending || (temp->searchForTarget) || temp->myPPCBlock->currentSessionRef) {
  128.                     HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
  129.                     if (!temp->notSys7)
  130.                         FindAEBuddy((CDEVHnd)cdevStorage);
  131.                 } else {
  132.                     /* if the buddy is not up yet, or if it could not be found at all */
  133.                     /* dim the button out. */
  134.                     if (temp->noBuddy || temp->notSys7)
  135.                         HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
  136.                     else
  137.                         HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 0);
  138.                 }
  139.                 /* also, there's .... I forgot what I was going to say */
  140.                 /* Oh yeah, I remember.  I also need to check here periodically to */
  141.                 /* see if the PPC calls have completed so I can dispose of the */
  142.                 /* data handle that I allocated for the PPC XFer process. */
  143.                 if (!temp->myPPCBlock->pB.openParam.portRefNum && temp->myPPCBlock->buffer != nil && !temp->noBuddy) {
  144.                     DisposPtr(temp->myPPCBlock->buffer);
  145.                     temp->myPPCBlock->buffer = nil;
  146.                     temp->eventPending = false;
  147.                 }
  148.                 HUnlock((Handle)cdevStorage);
  149.                 break;
  150.                 
  151.             case hitDev:   /* handle hit on item */
  152.             /* They clicked in our button.  Find our Buddy ( it is possible that it  */
  153.             /* terminated while we were waiting, this will re-launch it if that happened ) */
  154.             
  155.                 if ((item - numItems) == kSendButton) {
  156.                     /* our send button.  Do it now */
  157.                     FindAEBuddy((CDEVHnd)cdevStorage);                    
  158.                     FindATarget((CDEVHnd)cdevStorage);
  159.                 }
  160.                 break;
  161.             case closeDev:                                  /* clean up and dispose */
  162.                 temp = (CDEVPtr)*cdevStorage;
  163.                 if (temp->myPPCBlock->ourPort) {
  164.                     /* we have a port open.  Close the port, blowing off any pending */
  165.                     /* writes or whatever */
  166.                     PPCClosePBPtr closeRec = NewPtrClear(sizeof(PPCClosePBRec));
  167.                     closeRec->ioCompletion = nil;
  168.                     closeRec->ioResult = 0;
  169.                     closeRec->portRefNum = temp->myPPCBlock->ourPort;
  170.                     PPCClose(closeRec, false);
  171.                     DisposPtr((Ptr)closeRec);
  172.                                 }
  173.                     /* and kill our memory */
  174.                     if (temp->myPPCBlock->myPort)DisposPtr((Ptr)temp->myPPCBlock->myPort);
  175.                     if (temp->myPPCBlock)DisposPtr((Ptr)temp->myPPCBlock);
  176.                     
  177.                 break;
  178.                 
  179.             case updateDev:                                 /* handle any update drawing */
  180.             case activDev:                                  /* activate any needed items */
  181.             case deactivDev:                                /* deactivate any needed items */
  182.                 break;
  183.                 
  184.             case keyEvtDev:                                 /* respond to keydown */
  185.                 break;
  186.                 
  187.             case macDev:
  188.             case undoDev:
  189.                 break;
  190.                 
  191.             case cutDev:
  192.             case copyDev:
  193.             case pasteDev:
  194.             case clearDev:
  195.                 break;
  196.         }
  197.         return(cdevStorage);
  198.     }  /* cdevStorage != nil */
  199.     
  200.     /* 
  201.     **  if cdevStorage = NIL then ControlPanel 
  202.     **  will put up memory error
  203.     */
  204.     return(nil);
  205. }
  206.  
  207. /* Gets the ControlHandle for the item you want in the dialog box thebox.  */
  208. /* Handy for setting checkboxes and radio buttons */
  209. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  210. {
  211.     short itemtype;
  212.     Rect itemrect;
  213.     Handle thandle;
  214.     GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  215.     return((ControlHandle)thandle);
  216. }
  217.  
  218.  
  219. #undef __BUILDINGCDEV__
  220. #undef __AECDEV__
  221.